package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.ActivityReportEnterpriseBinding;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.FilePictureEntity;
import com.sgb.kjwl.model.entity.pick.UserInquiryForTender;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.swgk.core.util.UserOperating;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.MemberEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.ReportImageAdapter;
import com.sgb.kjwl.view.widget.enterprise.AddContentListView;
import com.sgb.kjwl.viewmodel.enterprise.ReportEnterpriseModel;
import com.swgk.core.customview.CfkEditView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业服务举报
 */
public class ReportEnterpriseActivity extends AppActivity {

    //行贿受贿
    private static final int TYPE_BRIBERY = 1;
    //服务态度
    private static final int TYPE_SERVICE_STATUS = 2;
    //商业欺诈
    private static final int TYPE_FRAUD = 3;
    //违约纠纷
    private static final int TYPE_DISPUTE = 4;
    //工作作风
    private static final int TYPE_WORK_STYLE = 5;
    //工作纠偏
    private static final int TYPE_WORK_DEVIATION = 6;
    //其他
    private static final int TYPE_OTHER = 7;
    //集合
    private List<PickSelectEntity> reportList = new ArrayList<>();

    private int currentType = TYPE_BRIBERY;

    ReportEnterpriseModel viewModel;

    private ActivityReportEnterpriseBinding binding;
    private ReportImageAdapter adapter;
    private List<String> urls;
    private List<File> fileList;
    public int imgTotal;
    private int currentImgNum;
    private List<String> uploadImgList = new ArrayList<>();
    private List<UserInquiryForTender.AddCustomListBean> customFieldListBeans;
    private List<FilePictureEntity> fileImageList = new ArrayList<>();
    private String description;
    private String breachPrinciplePeople;
    private String reportingAppeal;
    private String affectConsequence;
    private String suggestedTreatment;
    private String requestScheme;
    private String deviationWork;
    private String reportingPeopleId;
    private String reportingPeopleName;
    private String reportingPeopleTel;
    private String reportingType = "1";
    private int reportingMatters;
    private String reportDetailUrl = "https://user.shigongbang.com/superviseList";

    private String processingResults;

    private String businessType = "0";
    private String enterpriseType = "";
    private String businessId = "";
    private String businessName = "";
    private String dealCompanyName = "";
    private String dealCompanyNo = "";
    private String dealTeamName = "";
    private String dealTeamNo = "";
    private String dealUserName = "";
    private String dealUserNo = "";
    //默认非匿名
    private boolean isAnonymity = false;
    private Map<String, Object> mMap = new HashMap<>();
    List<LocalMedia> selectList = new ArrayList<>();

    //创建观察者对象，用来观察图片被删除或新增之后数量的变化，用来记录数量
    private ReportImageAdapter.ReportObserver reportObserver = new ReportImageAdapter.ReportObserver() {
        @Override
        public void notifyItemSizeChange(int size) {
            if (size == 0) {
                resetVisibility(binding.tvNum, false);
                return;
            }
//            resetVisibility(binding.tvNum, true);
//            binding.tvNum.setText(size + " / 5");
        }
    };

    /**
     * @param context
     * @param businessType   业务类型 1 转让 2需求 3 服务 4 团队
     * @param enterpriseType 企业服务类型 1 服务大厅 2商标转让 3 专利转让 4 版权转让 5 公司转让 6资产转让 7需求大厅 8专业团队
     */
    public static void start(Context context, String businessId, String businessName, String businessType, String enterpriseType,
                             String dealCompanyName, String dealCompanyNo, String dealTeamName, String dealTeamNo, String dealUserName, String dealUserNo) {
        Intent intent = new Intent();
        intent.putExtra("businessId", businessId);
        intent.putExtra("businessName", businessName);
        intent.putExtra("businessType", businessType);
        intent.putExtra("enterpriseType", enterpriseType);
        intent.putExtra("dealCompanyName", dealCompanyName);
        intent.putExtra("dealCompanyNo", dealCompanyNo);
        intent.putExtra("dealTeamName", dealTeamName);
        intent.putExtra("dealTeamNo", dealTeamNo);
        intent.putExtra("dealUserName", dealUserName);
        intent.putExtra("dealUserNo", dealUserNo);
        intent.setClass(context, ReportEnterpriseActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        businessType = getIntent().getStringExtra("businessType");
        enterpriseType = getIntent().getStringExtra("enterpriseType");
        businessId = getIntent().getStringExtra("businessId");
        businessName = getIntent().getStringExtra("businessName");
        dealCompanyName = getIntent().getStringExtra("dealCompanyName");
        dealCompanyNo = getIntent().getStringExtra("dealCompanyNo");
        dealTeamName = getIntent().getStringExtra("dealTeamName");
        dealTeamNo = getIntent().getStringExtra("dealTeamNo");
        dealUserName = getIntent().getStringExtra("dealUserName");
        dealUserNo = getIntent().getStringExtra("dealUserNo");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_report_enterprise);
        binding.ivAddAns.setOnClickListener(this);
        binding.tvSubmit.setOnClickListener(this);
        binding.tvAnonymity.setOnClickListener(this);
    }

    @Override
    protected void init() {
        viewModel = new ViewModelProvider(this).get(ReportEnterpriseModel.class);
        initReportType();
        handleClickItem();
        viewModel.uploadSuccess.observe(this, this::uploadRefresh);
        viewModel.saveSuccess.observe(this, a -> {
            finish();
            EventBus.getDefault().postSticky(MemberEvent.make().setMessageTag(EventConstant.EVENT_REPORT_INQUIRE_FINISH));
        });
    }

    private void initReportType() {
        String[] reportTypeArray = getResources().getStringArray(R.array.report_type);
        for (int i = 0; i < reportTypeArray.length; i++) {
            String reportType = reportTypeArray[i];
            PickSelectEntity temp = new PickSelectEntity();
            temp.setNo(String.valueOf(i + 1));
            temp.setProjectscale(reportType);
            reportList.add(temp);
        }
        ReportTypeAdapter reportTypeAdapter = new ReportTypeAdapter(this, reportList);
        binding.reportList.setLayoutManager(new GridLayoutManager(this, 4));
        binding.reportList.setAdapter(reportTypeAdapter);
        reportTypeAdapter.setItemSelect(true);
        reportTypeAdapter.setDefItem(0);
        reportTypeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                currentType = Integer.valueOf(pickSelectEntity.getNo());
                handleClickItem();
            }
        });
    }

    private void handleClickItem() {
        reset();
        handleImg();
        handleField();
        switch (currentType) {
            case TYPE_BRIBERY://行贿受贿
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llReportingAppealContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, true);

                resetData(binding.tvReportingAppeal, "举报诉求：");
                resetData(binding.tvDescription, "违纪描述");
                resetData(binding.tvBreachPrinciplePeople, "违纪人员");

                setHintText(binding.cfkDescriptionEdit, "请输入违纪描述最少5个字，最多200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入违纪人员");
                setHintText(binding.cfkReportingAppealEdit, "最多输入200字");
                break;
            case TYPE_SERVICE_STATUS://服务态度
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llAffectConsequenceContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, true);

                resetData(binding.tvDescription, "过程描述");
                resetData(binding.tvBreachPrinciplePeople, "相关人员");
                resetData(binding.tvAffectConsequence, "影响后果：");
                resetData(binding.tvRequestScheme, "建议处理：");

                setHintText(binding.cfkDescriptionEdit, "请输入过程描述最少5个字，最多200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员");
                setHintText(binding.cfkAffectConsequenceEdit, "最多输入200字");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_FRAUD://商业欺诈
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, true);

                resetData(binding.tvDescription, "内容描述");
                resetData(binding.tvBreachPrinciplePeople, "相关人员");
                resetData(binding.tvRequestScheme, "要求方案：");

                setHintText(binding.cfkDescriptionEdit, "请输入内容描述最少5个字，最多200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_DISPUTE://违约纠纷
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, true);

                resetData(binding.tvDescription, "违纪描述");
                resetData(binding.tvBreachPrinciplePeople, "违纪人员");
                resetData(binding.tvRequestScheme, "要求方案：");

                setHintText(binding.cfkDescriptionEdit, "请输入违纪描述最少5个字，最多200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入违纪人员");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_WORK_STYLE://工作作风
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llAffectConsequenceContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, true);

                resetData(binding.tvDescription, "过程描述");
                resetData(binding.tvBreachPrinciplePeople, "相关人员");
                resetData(binding.tvAffectConsequence, "影响后果：");
                resetData(binding.tvSuggestedTreatment, "建议处理：");

                setHintText(binding.cfkDescriptionEdit, "请输入过程描述最少5个字，最多200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员");
                setHintText(binding.cfkAffectConsequenceEdit, "最多输入200字");
                setHintText(binding.cfkSuggestedTreatmentEdit, "最多输入200字");
                break;
            case TYPE_WORK_DEVIATION://工作纠偏
                resetVisibility(binding.llDeviationWorkContainer, true);
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, false);

                resetData(binding.tvDescription, "内容描述");
                resetData(binding.tvBreachPrinciplePeople, "相关人员：");
                resetData(binding.tvSuggestedTreatment, "我的建议：");

                setHintText(binding.cfkDescriptionEdit, "请输入内容描述最少5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "最多输入200字");
                setHintText(binding.cfkSuggestedTreatmentEdit, "最多输入200字");
                break;
            case TYPE_OTHER://其他
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);
                resetVisibility(binding.tvBreachPrinciplePeopleStar, false);

                resetData(binding.tvDescription, "内容描述");
                resetData(binding.tvBreachPrinciplePeople, "相关人员：");
                resetData(binding.tvRequestScheme, "要求方案：");

                setHintText(binding.cfkDescriptionEdit, "请输入内容描述最少5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
        }
    }

    private void handleImg() {
        urls = new ArrayList<>();
        fileList = new ArrayList<>();
        adapter = new ReportImageAdapter(this, null, null, 5);
        adapter.registerObserver(reportObserver);
        adapter.setDatas(null, urls);
        adapter.setFileList(fileList);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        binding.recyclerView.setAdapter(adapter);
    }

    private void handleField() {
        customFieldListBeans = new ArrayList<>();
        binding.ivAddAns.setOnClickListener(v -> {
            if (binding.rootLayout.getChildCount() < 5) {
                setAddUseable(false);
                if (binding.rootLayout.getChildCount() > 0) {
                    if (checkItemInput(true)) {
                        AddContentListView pickDetailListView = new AddContentListView(this);
                        binding.rootLayout.addView(pickDetailListView);
                        pickDetailListView.OnInputContent(new AddContentListView.IInputContent() {
                            @Override
                            public void hasContent() {
                                if (binding.rootLayout.getChildCount() < 5)
                                    setAddUseable(true);
                            }

                            @Override
                            public void hasNoContent() {
                                setAddUseable(false);
                            }
                        });
                        pickDetailListView.setItemChildCLickListener(new AddContentListView.ItemChildCLickListener() {
                            @Override
                            public void deletePos() {
                                binding.rootLayout.removeView(pickDetailListView);
                                setAddUseable(checkItemInput(false));
                            }
                        });
                    }
                } else {
                    AddContentListView pickDetailListView = new AddContentListView(this);
                    binding.rootLayout.addView(pickDetailListView);
                    pickDetailListView.OnInputContent(new AddContentListView.IInputContent() {
                        @Override
                        public void hasContent() {
                            if (binding.rootLayout.getChildCount() < 5)
                                setAddUseable(true);
                        }

                        @Override
                        public void hasNoContent() {
                            setAddUseable(false);
                        }
                    });
                    pickDetailListView.setItemChildCLickListener(new AddContentListView.ItemChildCLickListener() {
                        @Override
                        public void deletePos() {
                            binding.rootLayout.removeView(pickDetailListView);
                            setAddUseable(checkItemInput(false));
                        }
                    });
                }

            } else {
                MToast.showLongToast(getApplicationContext(), "最多添加5条");
            }
        });
    }

    private boolean checkItemInput(boolean isShowToast) {
        for (int i = 0; i < binding.rootLayout.getChildCount(); i++) {
            AddContentListView pickLetterListView = (AddContentListView) binding.rootLayout.getChildAt(i);
            if (pickLetterListView.getViewContent()) {
                if (isShowToast) {
//                    MToast.showLongToast(getApplicationContext(), "有新增的标题和内容未填写");
                    DialogHelper.showSingleDialog(this, "", "新增的标题与内容全部填写", "确定", false, new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });

                }
                return false;
            }
        }
        return true;
    }

    /**
     * 设置新增按钮是否可点击
     *
     * @param useable true可用
     */
    public void setAddUseable(boolean useable) {
        if (useable) {
            binding.ivAddAns.setAlpha(1);
            binding.ivAddAns.setClickable(true);
        } else {
            binding.ivAddAns.setAlpha(0.5f);
            binding.ivAddAns.setClickable(false);
        }
    }

    private void reset() {
        resetVisibility(binding.llDescriptionContainer, false);
        resetVisibility(binding.llBreachPrinciplePeopleContainer, false);
        resetVisibility(binding.llReportingAppealContainer, false);
        resetVisibility(binding.llAffectConsequenceContainer, false);
        resetVisibility(binding.llSuggestedTreatmentContainer, false);
        resetVisibility(binding.llRequestSchemeContainer, false);
        resetVisibility(binding.llDeviationWorkContainer, false);
        resetVisibility(binding.tvNum, false);

        resetData(binding.cfkDeviationWorkEdit, "");
        resetData(binding.cfkAffectConsequenceEdit, "");
        resetData(binding.cfkBreachPrinciplePeopleEdit, "");
        resetData(binding.cfkDescriptionEdit, "");
        resetData(binding.cfkReportingAppealEdit, "");
        resetData(binding.cfkRequestSchemeEdit, "");
        resetData(binding.cfkSuggestedTreatmentEdit, "");
        resetData(binding.tvNum, "");

        resetData();

    }

    private void resetData() {
        description = null;
        breachPrinciplePeople = null;
        reportingAppeal = null;
        affectConsequence = null;
        suggestedTreatment = null;
        requestScheme = null;
        deviationWork = null;
        reportingPeopleId = null;
        reportingPeopleName = null;
        reportingPeopleTel = null;
        reportingType = "1";
        reportingMatters = 0;
        reportDetailUrl = null;
        processingResults = null;

    }

    private void resetVisibility(View view, boolean isShow) {
        if (view != null) {
            view.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    private void resetData(TextView view, String text) {
        if (view != null) {
            view.setText(text);
        }
    }

    private void setHintText(CfkEditView view, String text) {
        CfkEditView.setHintText(view, text);
    }

    private void resetData(CfkEditView view, String text) {
        if (view != null) {
            view.setEditContent(text);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_anonymity:
                binding.tvAnonymity.setSelected(isAnonymity = !isAnonymity);
                break;
            case R.id.tv_submit:
                if (!ClickUtils.isDoubleClick()) {
                    resetData();
                    if (isCanSubmit()) {
                        if (!checkItemInput(true)) {
                            return;
                        }
                        reportingMatters = currentType;
                        reportingType = isAnonymity ? "2" : "1";

                        if (adapter.getFileList().size() != 0) {
                            imgTotal = adapter.getFileList().size();
                            viewModel.uploadMutiFileModel(this, adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
                        } else {
                            uploadImgSuccess(adapter.getUrls(), 1);
                        }
                    }
                }
                break;
        }
    }

    private boolean isCanSubmit() {
        if (currentType == TYPE_BRIBERY) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入违纪描述最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入违纪人员");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            reportingAppeal = binding.cfkReportingAppealEdit.getEditContent();
        } else if (currentType == TYPE_SERVICE_STATUS) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入过程描述最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            affectConsequence = binding.cfkAffectConsequenceEdit.getEditContent();
            reportingAppeal = binding.cfkReportingAppealEdit.getEditContent();
        } else if (currentType == TYPE_FRAUD) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入内容描述最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        } else if (currentType == TYPE_DISPUTE) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入违纪描述最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入违纪人员");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        } else if (currentType == TYPE_WORK_STYLE) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入过程描述最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            affectConsequence = binding.cfkAffectConsequenceEdit.getEditContent();
            suggestedTreatment = binding.cfkSuggestedTreatmentEdit.getEditContent();
        } else if (currentType == TYPE_WORK_DEVIATION) {
            if (TextUtils.isEmpty(binding.cfkDeviationWorkEdit.getEditContent()) || binding.cfkDeviationWorkEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入走偏工作最少5个字最多200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入内容描述最少5个字最多200字");
                return false;
            }

//            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
//                MToast.showToast(getApplicationContext(), "请输入相关人员");
//                return false;
//            }
            description = binding.cfkDescriptionEdit.getEditContent();
            deviationWork = binding.cfkDeviationWorkEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            suggestedTreatment = binding.cfkSuggestedTreatmentEdit.getEditContent();
        } else if (currentType == TYPE_OTHER) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入内容描述最少5个字最多200字");
                return false;
            }

//            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
//                MToast.showToast(getApplicationContext(), "请输入相关人员");
//                return false;
//            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        }
        reportingPeopleId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        return true;
    }

    public void initMap() {
        switch (currentType) {
            case TYPE_BRIBERY:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("reportContent", reportingAppeal);
                break;
            case TYPE_SERVICE_STATUS:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("consequences", affectConsequence);
                mMap.put("reportContent", reportingAppeal);
                break;
            case TYPE_FRAUD:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("reportContent", requestScheme);
                break;
            case TYPE_DISPUTE:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("reportContent", requestScheme);
                break;
            case TYPE_WORK_STYLE:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("consequences", affectConsequence);
                mMap.put("reportContent", suggestedTreatment);
                break;
            case TYPE_WORK_DEVIATION:
                mMap.put("content", description);
                mMap.put("partialWork", deviationWork);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("reportContent", suggestedTreatment);
                break;
            case TYPE_OTHER:
                mMap.put("content", description);
                mMap.put("disciplinaryPersonnel", breachPrinciplePeople);
                mMap.put("reportContent", requestScheme);
                break;
        }
        mMap.put("reportType", currentType);
        mMap.put("filedList", GsonManager.getInstance().getGson().toJson(fileImageList));
        mMap.put("customFieldList", GsonManager.getInstance().getGson().toJson(customFieldListBeans));
        mMap.put("anonymous", binding.tvAnonymity.isSelected() ? 1 : 0);
        mMap.put("reportTel", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, ""));
        mMap.put("businessId", businessId);
        mMap.put("businessName", businessName);
        mMap.put("dealCompanyName", dealCompanyName);
        mMap.put("dealCompanyNo", dealCompanyNo);
        mMap.put("dealTeamName", dealTeamName);
        mMap.put("dealTeamNo", dealTeamNo);
        mMap.put("dealUserName", dealUserName);
        mMap.put("dealUserNo", dealUserNo);
        mMap.put("businessType", businessType);
        mMap.put("enterpriseType", enterpriseType);

        if (UserOperating.getInstance().isCompany()) {
            mMap.put("reportCompanyName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME, ""));
            mMap.put("reportCompanyNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, ""));
        } else {
            mMap.put("reportUserName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME, ""));
            mMap.put("reportUserNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""));
        }
    }

    private void uploadImgSuccess(List<String> urls, int type) {
        if (type == 0) {
            //拼接图片url
            for (int i = 0; i < urls.size(); i++) {
                FilePictureEntity fileListBean = new FilePictureEntity();
                int picI = i + 1;
                fileListBean.setName("第" + picI + "张图片");
                fileListBean.setUrl(urls.get(i));
                fileImageList.add(fileListBean);
            }
        }
        for (int i = 0; i < binding.rootLayout.getChildCount(); i++) {
            AddContentListView pickLetterListView = (AddContentListView) binding.rootLayout.getChildAt(i);
            customFieldListBeans.add(pickLetterListView.getContent());
        }

        initMap();
        viewModel.reportAdd(this, mMap);
    }


    //图片单张上传
    public void uploadRefresh(String url) {
        uploadImgList.add(url);
        if (currentImgNum + 1 == imgTotal) {
            uploadImgSuccess(uploadImgList, 0);
        } else {
            currentImgNum++;
            viewModel.uploadMutiFileModel(this, adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调
                selectList = PictureSelector.obtainMultipleResult(data);
                fileList.clear();
                urls.clear();
                for (LocalMedia localMedia : selectList) {
                    File file = new File(localMedia.getRealPath());
                    fileList.add(file);
                    urls.add(localMedia.getRealPath());
                }
                adapter.setDatas(selectList, urls);
                adapter.setFileList(fileList);
            }
        }
    }

    @Override
    public void finish() {
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
        super.finish();
    }

    @Override
    protected void onDestroy() {
        if (adapter != null) {
            adapter.unRegisterObserver(reportObserver);
        }
        super.onDestroy();
    }


    public class ReportTypeAdapter<T> extends BaseRecycleAdapter<T> {

        public ReportTypeAdapter(Context context, List<T> datas) {
            super(context, R.layout.item_report_enterprise_type, datas);
        }

        @Override
        public void convert(ItemViewHolder holder, T s) {
            holder.setBinding(BR.viewModel, s);
        }
    }

}
